package com.juyu.miliao.common.dataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class MultipleSqlSessionFactoryParser {
    private final SqlSessionFactory defaultSqlSessionFactory;
    private final Map<Object, SqlSessionFactory> sqlSessionFactoryMap;

    private MultipleSqlSessionFactoryParser(SqlSessionFactory defaultSqlSessionFactory, Map<Object, SqlSessionFactory> sqlSessionFactoryMap){
        this.defaultSqlSessionFactory = defaultSqlSessionFactory;
        this.sqlSessionFactoryMap = sqlSessionFactoryMap;
    }

    public SqlSessionFactory getDefaultSqlSessionFactory() {
        return defaultSqlSessionFactory;
    }

    public Map<Object, SqlSessionFactory> getSqlSessionFactoryMap() {
        return sqlSessionFactoryMap;
    }

    public static Builder builder(){
        return new Builder();
    }

    @FunctionalInterface
    public interface SqlSessionFactoryCreater{

        public SqlSessionFactory create(Object key, Object DataSource);

    }

    public static class Builder {
        private final Logger logger = LoggerFactory.getLogger(MultipleSqlSessionFactoryParser.Builder.class);
        private MultipleDataSourceParser multipleDataSourceParser;
        private SqlSessionFactoryCreater sqlSessionFactoryCreater;

        public MultipleDataSourceParser getMultipleDataSourceParser() {
            return multipleDataSourceParser;
        }

        public Builder setMultipleDataSourceParser(MultipleDataSourceParser multipleDataSourceParser) {
            this.multipleDataSourceParser = multipleDataSourceParser;
            return this;
        }

        public SqlSessionFactoryCreater getSqlSessionFactoryCreater() {
            return sqlSessionFactoryCreater;
        }

        public Builder setSqlSessionFactoryCreater(SqlSessionFactoryCreater sqlSessionFactoryCreater) {
            this.sqlSessionFactoryCreater = sqlSessionFactoryCreater;
            return this;
        }

        public MultipleSqlSessionFactoryParser build(){
            Map<Object, Object> dataSourceMap = multipleDataSourceParser.getDataSourceMap();
            Object defaultDataSource = multipleDataSourceParser.getDefaultDataSource();
            SqlSessionFactory defaultSqlSessionFactory = null;
            Map<Object, SqlSessionFactory> sqlSessionFactoryMap = new HashMap<>();
            for(Map.Entry<Object, Object> entry : dataSourceMap.entrySet()){
                SqlSessionFactory sqlSessionFactory = sqlSessionFactoryCreater.create(entry.getKey(), entry.getValue());
                if(logger.isDebugEnabled())
                    logger.debug("创建 sqlSessionFactory sqlSessionFactory=" + sqlSessionFactory + "， key=" + entry.getKey() + "，dateSource=" + entry.getValue());
                if(entry.getValue() == defaultDataSource) {
                    defaultSqlSessionFactory = sqlSessionFactory;
                    if(logger.isDebugEnabled())
                        logger.debug("默认 sqlSessionFactory sqlSessionFactory=" + sqlSessionFactory + "， key=" + entry.getKey() + "，dateSource=" + entry.getValue());
                }
                sqlSessionFactoryMap.put(entry.getKey(), sqlSessionFactory);
            }
            return new MultipleSqlSessionFactoryParser(defaultSqlSessionFactory, sqlSessionFactoryMap);
        }

    }

}
